﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InternalManagementSystem.DAO.Models;
using System.Data;

public partial class Users_Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (!permission.IsDeductOperate)//扣分操作
                {
                    btnSave.Visible = false;
                    
                }

                if (Request.Params["uid"] != null)
                {
                    try
                    {
                        int uid = Convert.ToInt32(Request.Params["uid"]);
                        LoadUser(uid);
                      
                    }
                    catch
                    {
                    }
                }
            }


        }
    }

    void LoadUser(int uid)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            UserBLL bll = new UserBLL(permission);
            DataSet ds = bll.GetUserView(uid);
            if(ds!=null&&ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                lblCompany.Text = SwiftConverter.CovertDBObjectToString(dr["CompanyName"]);
                lblCredential.Text = SwiftConverter.CovertDBObjectToString(dr["Credential"]);
                lblCurrentIntegral.Text = SwiftConverter.CovertDBObjectToString(dr["CurrentIntegral"]);
                lblIDCard.Text = SwiftConverter.CovertDBObjectToString(dr["IDCard"]);
                lblSex.Text = SwiftConverter.CovertDBObjectToMaleOrFemale(dr["Sex"]);
                lblUserName.Text = SwiftConverter.CovertDBObjectToString(dr["UserName"]);
                lblUserType.Text = SwiftConverter.CovertDBObjectToString(dr["UserTypeName"]);

                cbSafe.Checked = Convert.ToInt32(dr["IsSafetyEducation"]) == 1 ? true : false;
                cbfine.Checked = Convert.ToInt32(dr["IsFine"]) == 1 ? true : false;
                cbAutoDeduct.Checked = Convert.ToInt32(dr["IsAutoDeductPoints"]) == 1 ? true : false;
                txtAutoDeductPeriod.Text = SwiftConverter.CovertDBObjectToString(dr["AutoDeductPeriod"]);
                txtAutoDeductPoints.Text = SwiftConverter.CovertDBObjectToString(dr["AutoDeductPoints"]);
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            UserBLL bll = new UserBLL(permission);

            if (Request.Params["uid"] != null && Request.Params["vid"] != null)
            {
                int uid = Convert.ToInt32(Request.Params["uid"]);
                int vid = Convert.ToInt32(Request.Params["vid"]);
                bll.DoDeduct(vid, uid
                    , SwiftConverter.CovertStringToInt(txtUserIntegral.Text)
                    , SwiftConverter.CovertStringToInt(txtCompanyIntegral.Text), txtReason.Text);

                Staff user = bll.GetUser(uid);

                user.IsSafetyEducation = cbSafe.Checked ? 1 : 0;
                user.IsFine = cbfine.Checked ? 1 : 0;

                user.IsAutoDeductPoints = cbAutoDeduct.Checked ? 1 : 0;
                user.AutoDeductPeriod = string.IsNullOrEmpty(txtAutoDeductPeriod.Text) ? 0 : Convert.ToInt32(txtAutoDeductPeriod.Text);
                user.AutoDeductPoints = string.IsNullOrEmpty(txtAutoDeductPoints.Text) ? 0 : Convert.ToInt32(txtAutoDeductPoints.Text);

                permission.IsUserEdit = true;//临时赋予
                bll.UpdateUser(user);
                permission.IsUserEdit = false;
            }
        }

        Response.Redirect("~/Violation/Index.aspx");

    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Violation/Index.aspx");
    }
}
